package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcExamdicEntity;
import com.dhcc.cdc.busi.service.ICdcExamdicService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 检查项目字典
 * 
 * @ClassName: ExamitemController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:22:55
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcexamdic")
public class CdcExamdicController extends BaseController {

	private String PREFIX = "/backend/cdc/examdic/";

	@Autowired
	private ICdcExamdicService examdicService;
	

    /**
     * 跳转到检查项目字典列表首页
     */
    @RequestMapping("")
    @Permission("examdic:list")
    public String index() {
        return PREFIX + "examdic.html";
    }
	
    /**
     * 跳转到新增检查项目字典
     */
    @RequestMapping("/to_add")
    @Permission("examdic:add")
    public String toAdd() {
        return PREFIX + "examdic_add.html";
    }
	
	/**
     * 跳转到修改检查项目字典
     */
    @RequestMapping("/to_update/{examdicId}")
    @Permission("examdic:update")
    public String toUpdate(@PathVariable Long examdicId, Model model) {
        CdcExamdicEntity examdic = this.examdicService.selectById(examdicId);
        model.addAttribute("examdic",examdic);
        return PREFIX + "examdic_edit.html";
    }

    /**
     * 获取检查项目字典列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("examdic:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcExamdicEntity> page = new PageFactory<CdcExamdicEntity>().defaultPage();
        List<CdcExamdicEntity> result = this.examdicService.page(page,params);
        page.setRecords((List<CdcExamdicEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增检查项目字典
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("examdic:add")
    @BussinessLog(value = "新增检查项目字典")
    public Object add(CdcExamdicEntity examdic) {
        if (CommonUtil.isOneEmpty(examdic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        examdic.insert();
        return R.ok();
    }

    /**
     * 删除检查项目字典
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("examdic:delete")
    @BussinessLog(value = "删除检查项目字典")
    public Object delete(@RequestParam Long examdicId) {
	
        this.examdicService.deleteById(examdicId);

        return R.ok();
    }

    /**
     * 修改检查项目字典
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("examdic:update")
    @BussinessLog(value = "修改检查项目字典")
    public Object update(CdcExamdicEntity examdic) {
        if (CommonUtil.isOneEmpty(examdic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcExamdicEntity old = this.examdicService.selectById(examdic.getId());

        old.setCode(examdic.getCode());
        old.setDescription(examdic.getDescription());
        old.setIsActive(examdic.getIsActive());
        old.setDataId(examdic.getDataId());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
